#include "TheadFunctions.h"
#include "CommonFunction.h"


HANDLE globalThread1 = NULL;
HANDLE globalThread2 = NULL;

static KEVENT gEvent = { 0 };
bool fuckout = true;


VOID
DpcCallBack(
	_In_ struct _KDPC *Dpc,
	_In_opt_ PVOID DeferredContext,
	_In_opt_ PVOID SystemArgument1,
	_In_opt_ PVOID SystemArgument2
) {

	KdPrint(("[myddk] DpcCallBack IRQL:%d and CurProcess is %s", KeGetCurrentIrql(), GetCurrentProcessName()));
	return;
}


void UpIrqLevel() {
	static KSPIN_LOCK spinLock;
	KIRQL oldirql = 0;

	KeAcquireSpinLock(&spinLock, &oldirql);

	KdPrint(("[myddk] IRQL is %d", KeGetCurrentIrql()));

	KeReleaseSpinLock(&spinLock, oldirql);

	static KDPC dpc = { 0 };

	KeInitializeDpc(&dpc, DpcCallBack, NULL);

	KeInsertQueueDpc(&dpc, NULL, NULL);
}


void SubThread2(void *pCxt) {

	PKEVENT pEvent = (KEVENT*)pCxt;

	while (TRUE )
	{
		ShuiMian(3);
		KdPrint(("[myddk] setEvent"));
		KeSetEvent(pEvent, IO_NO_INCREMENT, FALSE);
	}
	PsTerminateSystemThread(0);
}


void SubThread1(void *pCxt) {
	KeInitializeEvent(&gEvent, NotificationEvent, FALSE);

	NTSTATUS status = PsCreateSystemThread(&globalThread2, 0, NULL, NULL, NULL, SubThread2, &gEvent);
	while (true)
	{
		KeWaitForSingleObject(&gEvent, Executive, KernelMode, FALSE, NULL);
		KeResetEvent(&gEvent);
		KdPrint(("[myddk] Event Has be setted"));
	}
	PsTerminateSystemThread(0);
}


void SubThread3(void *pCxt) {

	PKEVENT pEvent = (PKEVENT)pCxt;

	while (true)
	{
		KeWaitForSingleObject(pEvent, Executive, KernelMode, FALSE, NULL);
		KdPrint(("[myddk] Event Has be setted"));
	}
	PsTerminateSystemThread(0);
}